package yxy.game.pm2.command.altar;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.magicdata;
import yxy.cherry.data.bean.magicshop;
import yxy.cherry.data.bean.souldata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.AltarMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.*;

/** 祭坛 武将一键献祭 */
@MessageType(proto = Command.AltarHeroOneButton, classtype = AltarMessage.AltarHeroOneButtonRequest.class)
public class CMD_AltarHeroOneButton extends Command<AltarMessage.AltarHeroOneButtonRequest> {

	@Override
	public void execute(MessageSource messageSource, AltarMessage.AltarHeroOneButtonRequest request) {
		String playerCode = messageSource.getTag();

		AltarMessage.AltarHeroOneButtonResponse.Builder response = AltarMessage.AltarHeroOneButtonResponse.newBuilder();

		HeroHandle heroHandle = new HeroHandle();
		ArrayList<Currency> back = new ArrayList<>();
		Collection<String> heroCodes = heroHandle.getHeroAltarQuick(playerCode);
		//武将献祭前，脱下装备
		try {
			Currencies currencies = unfixEquip(playerCode, heroCodes, back);
			//将装备放回背包
			back.addAll(currencies);
		}catch (Exception e){
			e.printStackTrace();
		}
		//兵法 转换为 魂玉精华
		Currencies hunYu = unfixArtOfWar(playerCode, heroCodes);
		back.addAll(hunYu);

		//魂玉 转 魂玉精华
		Currencies hunYuCurrencies = unfixSoul(playerCode, heroCodes);
		back.addAll(hunYuCurrencies);

		Result result = heroHandle.getHeroAltarBack(playerCode, heroHandle.getHeroAltarQuick(playerCode), back);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		response.addAllBack(new CurrencyHandle().packItems(back));
		send(messageSource, response, Result.Success);
	}

	/** 献祭前将武将的装备放回背包 */
	private Currencies unfixEquip(String playerCode, Collection<String> heroCodes, ArrayList<Currency> back) throws Exception {
		Currencies temp = new Currencies();
		Iterator<String> iterator = heroCodes.iterator();
		while (iterator.hasNext()){
			String heroCode = iterator.next();
			Hero hero = Hero.get(playerCode, heroCode);
			//获取武将的装备
			int[] equips = hero.getEquips();
			for (int i = 0; i < equips.length; i++) {
				//值为零，该部位没有装备
				if (equips[i] == 0){
					continue;
				}
				Currency currency = Item.get(playerCode, CurrencyType.CT_装备, equips[i]);
				currency.setNumber(1);
				temp.add(currency);
			}
		}
		return temp;
	}

	/** 献祭前将武将当前使用的兵法 100% 转换为对应的魂玉精华*/
	private Currencies unfixArtOfWar(String playerCode, Collection<String> heroCodes){
		Currencies temp = new Currencies();
		Iterator<String> iterator = heroCodes.iterator();
		while (iterator.hasNext()){
			String heroCode = iterator.next();
			Hero hero = Hero.get(playerCode, heroCode);
			String magicA = hero.getMagicA();
			String magicB = hero.getMagicB();
			// 所有魂玉精华 的map
			Map<String, magicshop> hunYuMap = DataCenter.getAllData(magicshop.class);
			Set<Map.Entry<String, magicshop>> entries = hunYuMap.entrySet();

			if (magicA != null){
				//兵法槽A
				int hunYu1 = conversion(entries, magicA);
				temp.add(Currency.create(CurrencyType.CT_物品, 1010,hunYu1));
			}
			if (magicB != null){
				//兵法槽B
				int hunYu2 = conversion(entries, magicB);
				temp.add(Currency.create(CurrencyType.CT_物品, 1010,hunYu2));
			}
		}
		return temp;
	}

	/** 灵符 转换  魂玉精华*/
	private int conversion(Set<Map.Entry<String, magicshop>> entries, String magic){
		int hunYuNumber = 0;
		Iterator<Map.Entry<String, magicshop>> hunYuIterator = entries.iterator();
		while (hunYuIterator.hasNext()){
			Map.Entry<String, magicshop> hunYuEntry = hunYuIterator.next();
			// 兵法槽为空
			if (magic == null){
				break;
			}
			// 兵法槽
			magicdata magicdata = DataCenter.getData(Integer.parseInt(magic), yxy.cherry.data.bean.magicdata.class);
			if (magic != null && hunYuEntry.getValue().getShopid() == magicdata.getCostid()[0]){
				int hunYu = hunYuEntry.getValue().getCostnum(); // 兵法商店中 灵符对应的魂玉数
				int lingFu = magicdata.getCostnum()[0]; //灵符数量
				int jingHua = magicdata.getCostnum()[1]; //兵法升级  所需的魂玉精华数量
				//currency = Currency.create(CurrencyType.CT_物品, hunYuEntry.getValue().getCostid(), hunYu * lingFu + jingHua);
				hunYuNumber = hunYu * lingFu + jingHua;
			}
		}
		return hunYuNumber;
	}

	/** 魂玉 转换 魂玉精华 */
	private Currencies unfixSoul(String playerCode, Collection<String> heroCodes){
		Currencies temp = new Currencies();
		Iterator<String> iterator = heroCodes.iterator();
		while (iterator.hasNext()){
			String heroCode = iterator.next();
			Hero hero = Hero.get(playerCode, heroCode);
			String soulA = hero.getSoulA();
			String soulB = hero.getSoulB();

			if (soulA != null){
				Soul soul = Soul.get(playerCode, soulA);
				souldata soulAData = DataCenter.getData(soul.getDataId(), souldata.class);
				Currency currency = Currency.create(soulAData.getResolvetype(), soulAData.getResolveid(), soulAData.getResolvenum());
				temp.add(currency);
			}
			if (soulB != null){
				Soul soul = Soul.get(playerCode, soulB);
				souldata soulBData = DataCenter.getData(soul.getDataId(), souldata.class);
				Currency currency = Currency.create(soulBData.getResolvetype(), soulBData.getResolveid(), soulBData.getResolvenum());
				temp.add(currency);
			}
		}
		return temp;
	}

	private void send(MessageSource messageSource, AltarMessage.AltarHeroOneButtonResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}